﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class TransportController : ControllerBase
    {
        private OrderApp _orderApp;
        private CustomerApp _customerApp;
        private InvoiceApp _invoiceApp;
        private OperateRecordApp _operateRecordApp;
        public TransportController()
        {
            _orderApp = new OrderApp();
            _customerApp = new CustomerApp();
            _invoiceApp = new InvoiceApp();
            _operateRecordApp = new OperateRecordApp();
        }

        // GET: Transport
        public ActionResult Index(Guid inId)
        {
            Invoice invoice = _invoiceApp.GetSingleInvoice(inId);
            Order order = _orderApp.GetInfo(invoice.OrderId);
            Customer customer = _customerApp.GetInfo(order.CustomeId.Value);
            InvoceType invoceType = _invoiceApp.GetSingleInvoceType(invoice.Type);
            SendCar sc = _invoiceApp.GetSingleSendCar(invoice.Id);
            ExpressOrder exp = _invoiceApp.GetSingleExpressOrder(invoice.Id);

            ViewBag.InvoceTypeName = invoceType == null ? "" : invoceType.TypeName;
            ViewBag.CustomerShortName = customer.ShortName;
            ViewBag.Invoice = invoice;
            ViewBag.SendCar = sc;
            ViewBag.ExpressOrder = exp;
            return View(order);
        }

        public ActionResult SendCarQuery(int page = 1, string beginTime = "", string endTime = "", string number = "")
        {
            ViewBag.Number = number.Trim();
            DateTime _beginTime = DateTime.Parse("2000-01-01 10:10:10");
            DateTime _endTime = DateTime.Now.Date.AddMonths(3);
            ViewBag.beginTime = "";
            ViewBag.endTime = "";
            if (!string.IsNullOrEmpty(beginTime) && Validate.IsDate(beginTime))
            {
                _beginTime = DateTime.Parse(beginTime.Trim());
                ViewBag.beginTime = beginTime.Trim();
            }
            if (!string.IsNullOrEmpty(endTime) && Validate.IsDate(endTime))
            {
                _endTime = DateTime.Parse(endTime.Trim());
                ViewBag.endTime = endTime.Trim();
            }

            IPagedList<InvoiceView> tecjList = _invoiceApp.SendCarQuery(1, number, _beginTime, _endTime).ToPagedList(page, 30);
            return View(tecjList);
        }

        public ActionResult SendCarPrint(string strids)
        {
            if (!string.IsNullOrEmpty(strids))
            {
                List<InvoiceView> list = new List<InvoiceView>();
                string[] strClo = strids.Split(',');
                foreach (var item in strClo)
                {
                    if (Validate.IsGuidByParse(item))
                    {
                        InvoiceView web = _invoiceApp.GetSingleInvoiceView(new Guid(item));
                        if (web != null)
                        {
                            list.Add(web);
                        }
                        else
                        {
                            InvoiceView web1 = _invoiceApp.GetSingleInvoiceView1(new Guid(item));
                            if (web1!=null)
                            {
                                list.Add(web1);
                            }
                        }
                    }
                }
                ViewBag.GuidList = list;
            }
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string SendCarPrintComplete(string[] strids)
        {
            if (strids.Length > 0)
            {
                List<InvoiceView> list = new List<InvoiceView>();
                foreach (var item in strids)
                {
                    if (Validate.IsGuidByParse(item))
                    {
                        SendCar web = _invoiceApp.GetSingleSendCarById(new Guid(item));
                        web.Status = 1;
                        _invoiceApp.UpdateSendCar(web);
                    }
                }
                return new { code = 200 }.ToJson();
            }
            return new { code = -200 }.ToJson();
        }


        public ActionResult ExpressOrderQuery(int page = 1, string beginTime = "", string endTime = "", string number = "")
        {
            ViewBag.Number = number.Trim();
            DateTime _beginTime = DateTime.Parse("2000-01-01 10:10:10");
            DateTime _endTime = DateTime.Now.Date.AddDays(1);
            ViewBag.beginTime = "";
            ViewBag.endTime = "";
            if (!string.IsNullOrEmpty(beginTime) && Validate.IsDate(beginTime))
            {
                _beginTime = DateTime.Parse(beginTime.Trim());
                ViewBag.beginTime = beginTime.Trim();
            }
            if (!string.IsNullOrEmpty(endTime) && Validate.IsDate(endTime))
            {
                _endTime = DateTime.Parse(endTime.Trim());
                ViewBag.endTime = endTime.Trim();
            }

            IPagedList<InvoiceView> tecjList = _invoiceApp.ExpressOrderQuery(2, number, _beginTime, _endTime).ToPagedList(page, 30);
            return View(tecjList);
        }

        public ActionResult TransportExamine(int page = 1, string number = "", string takeCompany = "")
        {
            ViewBag.Number = number.Trim();
            ViewBag.TakeCompany = takeCompany.Trim();

            IPagedList<Invoice> tecjList = _invoiceApp.GetInvoiceListByStatus(101, number.Trim(), takeCompany.Trim()).ToPagedList(page, 10);
            return View(tecjList);
        }

        public ActionResult TransportExamineInfo(Guid inId)
        {
            Invoice invoice = _invoiceApp.GetSingleInvoice(inId);
            Order order = _orderApp.GetInfo(invoice.OrderId);
            Customer customer = _customerApp.GetInfo(order.CustomeId.Value);
            InvoceType invoceType = _invoiceApp.GetSingleInvoceType(invoice.Type);
            SendCar sc = _invoiceApp.GetSingleSendCar(invoice.Id);
            ExpressOrder exp = _invoiceApp.GetSingleExpressOrder(invoice.Id);
            IEnumerable<DeliveryProduct> dpList = _invoiceApp.GetListDeliveryProducts(inId);


            ViewBag.InvoceTypeName = invoceType == null ? "" : invoceType.TypeName;
            ViewBag.CustomerShortName = customer.ShortName;
            ViewBag.Invoice = invoice;
            ViewBag.SendCar = sc;
            ViewBag.ExpressOrder = exp;
            ViewBag.DeliveryProductList = dpList;


            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(order);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string UpdateInvoiceExamineType(Invoice web)
        {
            Invoice invoice = _invoiceApp.GetSingleInvoice(web.Id);
            invoice.Data = web.Data;
            invoice.Status = 2;
            invoice.ExamineMemo = web.ExamineMemo;
            _invoiceApp.Update(invoice);

            OperateRecord o = new OperateRecord();
            o.ItemId = invoice.Id;
            o.TypeId = 26;
            o.EndTime = DateTime.Now;
            o.CreatorId = operatorModel.UserId;
            o.CreatorName = operatorModel.FamilyName;
            _operateRecordApp.UpdateOperateRecord(o);//修改本操作人员工作记录日志

            return Result.ToJson();
        }

        public JsonResult DeleteIvoice(Guid id)
        {
            var res = 0;
            Invoice invoice = _invoiceApp.GetSingleInvoice(id);
            invoice.Status = -2;
            _invoiceApp.Update(invoice);

            OperateRecord o1 = new OperateRecord();
            o1.Id = Guid.NewGuid();
            o1.ItemId = id;
            o1.TypeId = 30;//取消发货单进行记录
            o1.StartTime = DateTime.Now;
            o1.EndTime = null;
            o1.CreatorId = null;
            _operateRecordApp.AddOperateRecord(o1);

            return Json(res);
        }

        public ActionResult InvoiceQuery(int page = 1, string number = "", string takeCompany = "")
        {
            ViewBag.Number = number.Trim();
            ViewBag.TakeCompany = takeCompany.Trim();

            IPagedList<Invoice> tecjList = _invoiceApp.GetInvoiceListByStatus(-1, number.Trim(), takeCompany.Trim()).ToPagedList(page, 10);
            return View(tecjList);
        }

        public ActionResult HasCancelInvoice(int page = 1, string number = "", string takeCompany = "")
        {
            ViewBag.Number = number.Trim();
            ViewBag.TakeCompany = takeCompany.Trim();

            IPagedList<Invoice> tecjList = _invoiceApp.GetInvoiceListByStatus(-2, number.Trim(), takeCompany.Trim()).ToPagedList(page, 10);
            return View(tecjList);
        }

        public ActionResult DeliverGoods(Guid inId)
        {
            Invoice invoice = _invoiceApp.GetSingleInvoice(inId);
            Order order = _orderApp.GetInfo(invoice.OrderId);
            Customer customer = _customerApp.GetInfo(order.CustomeId.Value);
            IEnumerable<InvoceType> invoceTypelist = _invoiceApp.GetInvoceTypeList();

            ViewBag.InvoceTypeList = invoceTypelist;
            ViewBag.CustomerShortName = customer.ShortName;
            ViewBag.Invoice = invoice;
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(order);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string UpdateInvoiceType(Guid invoicId)
        {
            DateTime nowTime = DateTime.Now;

            Invoice i = _invoiceApp.GetSingleInvoice(invoicId);
            i.Status = 101;

            _invoiceApp.Update(i);//修改发货单状态

            //插入给下一个工作人员添加人工记录（原料成本核算）
            OperateRecord o1 = new OperateRecord();
            o1.Id = Guid.NewGuid();
            o1.ItemId = i.Id;//原料成本Id
            o1.TypeId = 26;
            o1.StartTime = nowTime;
            o1.EndTime = null;
            o1.CreatorId = null;
            _operateRecordApp.AddOperateRecord(o1);


            return Result.ToJson();
        }
    }
}